package com.probox.common.entity.service.order.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.probox.common.core.constant.CachePrefix;
import com.probox.common.core.constant.Constants;
import com.probox.common.core.constant.MessageConstants;
import com.probox.common.core.enums.*;
import com.probox.common.core.exception.BaseException;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.StringUtils;
import com.probox.common.entity.domain.platfom.PlatformStore;
import com.probox.common.entity.service.platfom.IPlatformStoreService;
import com.probox.common.redis.service.RedisService;
import com.probox.common.entity.domain.product.ProductSku;
import com.probox.common.entity.service.product.IProductInfoService;
import com.probox.common.entity.service.product.IProductSkuService;
import com.probox.order.api.domain.OrderShopCartVo;
import com.probox.common.entity.domain.order.OrderShopCart;
import com.probox.common.entity.mapper.order.OrderShopCartMapper;
import com.probox.common.entity.service.order.IOrderInfoService;
import com.probox.common.entity.service.order.IOrderShopCartService;
import com.probox.common.entity.domain.order.vo.ShopCartVo;
import com.probox.product.api.domain.ProductInfoVo;
import com.probox.product.api.domain.SkuApi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * 购物车明细Service业务层处理
 *
 * @author hds
 * @date 2021-05-28
 */
@Service
@Transactional
public class OrderShopCartServiceImpl extends ServiceImpl<OrderShopCartMapper, OrderShopCart> implements IOrderShopCartService {
    @Autowired
    private OrderShopCartMapper orderShopCartMapper;

    @Autowired
    private IOrderInfoService orderInfoService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private IProductInfoService iProductInfoService;


    @Autowired
    private IProductSkuService iProductSkuService;

    @Autowired
    private IPlatformStoreService storeService;


    private final Logger logger = LoggerFactory.getLogger(OrderShopCartServiceImpl.class);


    /**
     * 查询购物车明细
     *
     * @param cartUid 购物车明细ID
     * @return 购物车明细
     */
    @Override
    public OrderShopCart selectOrderShopCartById(Long cartUid) {
        return this.getById(cartUid);
    }

    /**
     * 查询购物车明细列表
     *
     * @param orderShopCart 购物车明细
     * @return 购物车明细
     */
    @Override
    public List<OrderShopCart> queryList(OrderShopCart orderShopCart) {
        QueryWrapper<OrderShopCart> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(String.valueOf(orderShopCart.getCartUid())) && orderShopCart.getCartUid() != null) {
            queryWrapper.eq("CART_UID", orderShopCart.getCartUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderShopCart.getSupplierUid())) && orderShopCart.getSupplierUid() != null) {
            queryWrapper.eq("SUPPLIER_UID", orderShopCart.getSupplierUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderShopCart.getProUid())) && orderShopCart.getProUid() != null) {
            queryWrapper.eq("PRO_UID", orderShopCart.getProUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderShopCart.getProName())) && orderShopCart.getProName() != null) {
            queryWrapper.like("PRO_NAME", orderShopCart.getProName());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderShopCart.getSkuUid())) && orderShopCart.getSkuUid() != null) {
            queryWrapper.eq("SKU_UID", orderShopCart.getSkuUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderShopCart.getSkuDesc())) && orderShopCart.getSkuDesc() != null) {
            queryWrapper.like("SKU_DESC", orderShopCart.getSkuDesc());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderShopCart.getCartStatus())) && orderShopCart.getCartStatus() != null) {
            queryWrapper.eq("CART_STATUS", orderShopCart.getCartStatus());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderShopCart.getActivityUid())) && orderShopCart.getActivityUid() != null) {
            queryWrapper.eq("ACTIVITY_UID", orderShopCart.getActivityUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderShopCart.getActivityType())) && orderShopCart.getActivityType() != null) {
            queryWrapper.eq("ACTIVITY_TYPE", orderShopCart.getActivityType());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderShopCart.getActivityProUid())) && orderShopCart.getActivityProUid() != null) {
            queryWrapper.eq("ACTIVITY_PRO_UID", orderShopCart.getActivityProUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderShopCart.getCreateUid())) && orderShopCart.getCreateUid() != null) {
            queryWrapper.eq("CREATE_UID", orderShopCart.getCreateUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderShopCart.getUpdateUid())) && orderShopCart.getUpdateUid() != null) {
            queryWrapper.eq("UPDATE_UID", orderShopCart.getUpdateUid());
        }

        if(ObjectUtil.isNotEmpty(SecurityUtils.getStoreId())){
            queryWrapper.eq("store_id",SecurityUtils.getStoreId());
        }
        if (orderShopCart.getCreateTimeArray() != null && orderShopCart.getCreateTimeArray().size() > 0) {
            queryWrapper.ge("CREATE_TIME", orderShopCart.getCreateTimeArray().get(0));
            if (orderShopCart.getCreateTimeArray().size() > 1) {
                queryWrapper.le("CREATE_TIME", orderShopCart.getCreateTimeArray().get(1) + " 23:59:59");
            }
        }

        queryWrapper.orderByDesc("CREATE_TIME");

        Long platFormUid = SecurityUtils.getPlatformId();
        if (platFormUid != null && platFormUid > 0) {
            queryWrapper.eq("PLATFORM_UID", platFormUid);
        } else {
            throw new BaseException("账号异常");
        }
        return orderShopCartMapper.selectList(queryWrapper);
    }

    /**
     * 新增购物车明细
     *
     * @param orderShopCart 购物车明细
     * @return 结果
     */
    @Override
    public boolean insertOrderShopCart(OrderShopCart orderShopCart) {

        return this.save(orderShopCart);

    }

    /**
     * 修改购物车明细
     *
     * @param orderShopCart 购物车明细
     * @return 结果
     */
    @Override
    public boolean updateOrderShopCart(OrderShopCart orderShopCart) {

        return this.updateById(orderShopCart);
    }

    /**
     * 批量删除购物车明细
     *
     * @param cartUids 需要删除的购物车明细ID
     * @return 结果
     */
    @Override
    public boolean deleteOrderShopCartByIds(Long[] cartUids) {
        return this.removeByIds(Arrays.asList(cartUids));
    }


    //购物车限制用户的操作间隙
    private static final Long user_operator_limit_time = 1L;
    private static final Long user_operator_limit = 6L;
    //用户操作购物车的缓存
    private static final String user_operator_limit_key = CachePrefix.PRE_GLOABLE + "user_operator_cart_limit_";

    //限制单个用户的频繁操作
    private void userLimit() {
        String key = user_operator_limit_key + SecurityUtils.getCustomerUid();
        if (redisService.incr(key, user_operator_limit_time, TimeUnit.SECONDS) > user_operator_limit) {
            throw new BaseException(MessageConstants.MSG_FAST);
        }
    }

    /**
     * 查询已经加入到购车的商品
     *
     * @param proUid
     * @param skuId
     * @param customerUid
     * @return
     */
    private OrderShopCart getOrderShopCart(Long storeId,String proType,Long proUid, Long skuId, Long customerUid) {
        QueryWrapper<OrderShopCart> queryWrapper = Wrappers.query();
        queryWrapper.eq("PRO_UID", proUid);
        queryWrapper.eq("PRO_TYPE", proType);
        queryWrapper.eq("STORE_ID", storeId);
        if (skuId != null) {
            queryWrapper.eq("SKU_UID", skuId);
        }
        queryWrapper.eq("CREATE_UID", customerUid);
        queryWrapper.eq("IS_SHOW_IN_CART", CartIsShowEnum.show.getKey());
        queryWrapper.eq("CART_STATUS", CartStatusEnum.unPaid.getKey());
        queryWrapper.orderByDesc("CART_UID");
        List<OrderShopCart> list = orderShopCartMapper.selectList(queryWrapper);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    private String addWebP(String source){
        if (StringUtils.isNotEmpty(source)){
            if (source.indexOf(Constants.WEBP)<0){
                StringBuffer buffer = new StringBuffer();
                String[] banner = source.split(",");
                for (int i=0;i<banner.length;i++){
                    if (StringUtils.isNotEmpty(banner[i])){
                        banner[i] = banner[i]+Constants.WEBP;
                    }
                    if (i>0){
                        buffer.append(",");
                    }
                    buffer.append(banner[i]);
                }
                return buffer.toString();
            }
        }
        return source;
    }

    /**
     * 添加购物车操作
     *
     * @param shopCartVo
     * @return
     */
    @Override
    public ShopCartVo add(ShopCartVo shopCartVo,CartIsShowEnum showEnum) {
        userLimit();

        Long price = 0L;
        Long stock = 0L;

        try {

            PlatformStore store =  storeService.getById(shopCartVo.getStoreId());
            if (store == null || !store.getAuditStatus().equals(DataStatus.valid.getKey()) || !store.getDataStatus().equals(DataStatus.valid.getKey())){
                throw new BaseException("没有找到门店信息");
            }

          if (CardServiceTypeEnum.product.getKey().equals(shopCartVo.getProType())){
                ProductInfoVo productInfoApi = iProductInfoService.getProductDetail(shopCartVo.getProductId());
                if (ObjectUtil.isNotEmpty(productInfoApi)) {
                    SkuApi skuApi = null;
                    OrderShopCart cart = getOrderShopCart(shopCartVo.getStoreId(),shopCartVo.getProType(),shopCartVo.getProductId(), shopCartVo.getSkuId(), shopCartVo.getCustomerId());
                    if (cart == null) {
                        cart = new OrderShopCart();
                    }
                    cart.setIsShowInCart(showEnum.getKey());
                    cart.setProType(shopCartVo.getProType());
                    cart.setStoreId(shopCartVo.getStoreId());
                    if (shopCartVo.getSkuId() != null) {
                        skuApi = iProductInfoService.getSkuDetail(shopCartVo.getSkuId());
                        if (ObjectUtil.isEmpty(skuApi)) {
                            throw new BaseException("没有找到SKU信息");
                        }
                        if (skuApi.getProductUid().longValue() != productInfoApi.getProductUid()) {
                            throw new BaseException("商品与SKU信息不符");
                        }
                        cart.setPrice(skuApi.getSkuRetailPrice().longValue());
                        cart.setSkuUid(skuApi.getSkuUid());
                        cart.setSkuImgUrl(skuApi.getImgCoverUrl());
                        cart.setSkuDesc(skuApi.getValuesDesc());
                        if (skuApi.getStockNumber() < shopCartVo.getAmount()) {
                            throw new BaseException("商品库存只剩" + skuApi.getStockNumber() + "件");
                        }
                        if (!skuApi.getSkuStatus().equals(DataStatus.valid.getKey())) {
                            throw new BaseException("该商品SKU不能销售");
                        }
                    } else {
                        List<SkuApi> rst = null;
                        List<ProductSku> skus = iProductSkuService.getOnlineSkuList(shopCartVo.getProductId());
                        if (skus.size() > 0) {
                            rst = new ArrayList<SkuApi>();
                            SkuApi api = null;
                            for (ProductSku sku : skus) {
                                api = new SkuApi();
                                rst.add(api);
                                BeanUtils.copyProperties(sku, api);
                            }
                        }
                        if (ObjectUtil.isNotEmpty(rst)) {
                            throw new BaseException("请选择商品SKU再购买");
                        }
                        cart.setPrice(productInfoApi.getRetailPrice().longValue());
                        if (productInfoApi.getStockNumber() < shopCartVo.getAmount()) {
                            throw new BaseException("商品库存只剩" + productInfoApi.getStockNumber() + "件");
                        }
                    }
                    //如果有活动信息
                    cart.setActivityType(null);
                    cart.setActivityUid(null);
                    cart.setActivityProUid(null);
                    cart.setProName(productInfoApi.getProductName());
                    cart.setProImgUrl(productInfoApi.getcarouselImgOne());
                    cart.setProUid(productInfoApi.getProductUid());
                    cart.setProNo(productInfoApi.getProductNo());
                    cart.setProUnit(productInfoApi.getUnitName());
                    cart.setProTag(productInfoApi.getTagsName());
                    cart.setCartStatus(CartStatusEnum.unPaid.getKey());
                    if (cart.getCartUid() == null) {
                        cart.setCartNumber(shopCartVo.getAmount());
                        cart.setCreateUid(shopCartVo.getCustomerId());
                        cart.setCreateName(SecurityUtils.getCustomerUserName());
                        cart.setCreateTime(new Date());
                        cart.setUpdateName(cart.getCreateName());
                        cart.setUpdateTime(cart.getCreateTime());
                        cart.setUpdateUid(cart.getCreateUid());
                    } else {
                        cart.setCartNumber(shopCartVo.getAmount() + cart.getCartNumber());
                        cart.setUpdateName(SecurityUtils.getCustomerUserName());
                        cart.setUpdateTime(new Date());
                        cart.setUpdateUid(shopCartVo.getCustomerId());
                    }
                    cart.setSupplierPrice(productInfoApi.getCostPrice().longValue());
                    cart.setShowPrice(productInfoApi.getShowPrice().longValue());
                    cart.setCartAmount(cart.getCartNumber() * cart.getPrice());
                    cart.setPlatformUid(SecurityUtils.getCustomerPlatformUid());
                    cart.setScore(0L);
                    cart.setProImgUrl(addWebP(cart.getProImgUrl()));
                    cart.setSkuImgUrl(addWebP(cart.getSkuImgUrl()));
                    if (saveOrUpdate(cart)) {
                        shopCartVo.setCartId(cart.getCartUid());
                        //操作日志
                        if (ObjectUtil.isNotEmpty(cart.getCreateUid()))
                            orderInfoService.sendLogMsg(ProductOptType.addCar.getKey(), cart.getPlatformUid(), cart.getCreateUid(),cart.getProType(), cart.getProUid(), cart.getCartNumber());
                        return shopCartVo;
                    } else {
                        throw new BaseException("添加商品失败，请重试");
                    }
                }
                throw new BaseException("添加商品失败，请重试");
            }else{
                throw new BaseException("产品类型不正确");
            }

        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof BaseException) {
                throw e;
            } else {
                throw new BaseException("系统服务繁忙，请稍后再试！");
            }
        }

    }

    /**
     * 活动限购
     *
     * @param shopCartVo
     * @param limitsNumber
     */
    private void userBuyLimit(ShopCartVo shopCartVo, Integer limitsNumber) {
        if (limitsNumber <= 0) {
            return;
        }
        Long buyNumber = orderShopCartMapper.countActivityNumber(shopCartVo);
        if (buyNumber + shopCartVo.getAmount() > limitsNumber) {
            if (limitsNumber - buyNumber > 0) {
                throw new BaseException("每人限购" + limitsNumber + "件，您还可购买" + (limitsNumber - buyNumber) + "件");
            } else {
                throw new BaseException("每人限购" + limitsNumber + "件");
            }
        }
    }


    /**
     * 修改购物车数量
     *
     * @param shopCartVo
     * @return
     */
    @Override
    public ShopCartVo updateNumber(ShopCartVo shopCartVo) {
        userLimit();
        OrderShopCart cart = this.getById(shopCartVo.getCartId());
        if (cart != null && cart.getCreateUid().longValue() == SecurityUtils.getCustomerUid()) {
            if (cart.getCartStatus().equals(CartStatusEnum.unPaid.getKey())) {

                //Long oldNumber = cart.getCartNumber();
                cart.setCartNumber(shopCartVo.getAmount());
                cart.setCartAmount(cart.getCartNumber() * cart.getPrice());
                cart.setUpdateTime(new Date());

                if (saveOrUpdate(cart)) {
                    return shopCartVo;
                } else {
                    throw new BaseException("修改购物车数量失败，请稍后再试");
                }
            }
        }
        throw new BaseException("您不能修改购物车数量");
    }


    /**
     * 修改购物车SKU
     *
     * @param shopCartVo
     * @return
     */
    @Override
    public ShopCartVo updateSku(ShopCartVo shopCartVo) {
        userLimit();
        OrderShopCart cart = this.getById(shopCartVo.getCartId());
        if (cart != null && cart.getCreateUid().longValue() == SecurityUtils.getCustomerUid()) {
            if (cart.getCartStatus().equals(CartStatusEnum.unPaid.getKey())) {
                //如果SKU相同，则不修改
                if (shopCartVo.getSkuId().longValue() == cart.getSkuUid()) {
                    return shopCartVo;
                }

                //如果存在购物车信息，则直接删除当前的购物车信息，淘宝也是这么干的
                OrderShopCart shopCart = this.getOrderShopCart(shopCartVo.getStoreId(),cart.getProType(),shopCartVo.getProductId(), shopCartVo.getSkuId(), cart.getCreateUid());
                if (shopCart != null) {
                    cart.setUpdateTime(new Date());
                    cart.setCartStatus(CartStatusEnum.remove.getKey());
                    saveOrUpdate(cart);
                    return shopCartVo;
                } else {
                    //如果不存在，需要修改当前购物车的SKU信息，以当前SKU的价格信息或商品活动价格加入购物车

                    if (cart.getProType().equals(CardServiceTypeEnum.serviceItem.getKey())){

                    }else if (cart.getProType().equals(CardServiceTypeEnum.product.getKey())){
                    }
                }
            }
        }
        throw new BaseException("您不能修改购物车SKU");
    }

    /**
     * 删除购物车数量，只执行逻辑删除
     *
     * @param shopCartVo
     * @return
     */
    @Override
    public int delete(ShopCartVo shopCartVo) {
        userLimit();

        shopCartVo.setCustomerId(SecurityUtils.getCustomerUid());

        return orderShopCartMapper.delete(shopCartVo);
    }

    /**
     * 立即购买、秒杀、购买套餐的操作
     *
     * @param shopCartVo
     * @return
     */
    @Override
    public ShopCartVo buy(ShopCartVo shopCartVo) {
        userLimit();


            //立即购买、秒杀、积分兑换
            Long price = 0L;
            Long stock = 0L;


            try {

                ProductInfoVo productInfoApi = iProductInfoService.getProductDetail(shopCartVo.getProductId());
                if (ObjectUtil.isNotEmpty(productInfoApi)) {
                    SkuApi skuApi = null;
                    OrderShopCart cart = new OrderShopCart();
                    cart.setIsShowInCart(CartIsShowEnum.hide.getKey());
                    if (shopCartVo.getSkuId() != null) {
                        skuApi = iProductInfoService.getSkuDetail(shopCartVo.getSkuId());
                        if (ObjectUtil.isEmpty(skuApi)) {
                            throw new BaseException("没有找到SKU信息");
                        }
                        if (skuApi.getProductUid().longValue() != productInfoApi.getProductUid()) {
                            throw new BaseException("商品与SKU信息不符");
                        }
                        cart.setPrice(skuApi.getSkuRetailPrice().longValue());
                        cart.setSkuUid(skuApi.getSkuUid());
                        cart.setSkuImgUrl(skuApi.getImgCoverUrl());
                        cart.setSkuDesc(skuApi.getValuesDesc());
                        if (skuApi.getStockNumber() < shopCartVo.getAmount()) {
                            throw new BaseException("商品库存只剩" + skuApi.getStockNumber() + "件");
                        }
                        if (!skuApi.getSkuStatus().equals(DataStatus.valid.getKey())) {
                            throw new BaseException("该商品SKU不能销售");
                        }
                    } else {
                        List<SkuApi> rst = null;
                        List<ProductSku> skus = iProductSkuService.getOnlineSkuList(shopCartVo.getProductId());
                        if (skus.size() > 0) {
                            rst = new ArrayList<SkuApi>();
                            SkuApi api = null;
                            for (ProductSku sku : skus) {
                                api = new SkuApi();
                                rst.add(api);
                                BeanUtils.copyProperties(sku, api);
                            }
                        }
                        if (ObjectUtil.isNotEmpty(rst)) {
                            throw new BaseException("请选择商品SKU再购买");
                        }
                        cart.setPrice(productInfoApi.getRetailPrice().longValue());
                        if (productInfoApi.getStockNumber() < shopCartVo.getAmount()) {
                            throw new BaseException("商品库存只剩" + productInfoApi.getStockNumber() + "件");
                        }
                    }

                    cart.setScore(0L);

                    cart.setSupplierUid(productInfoApi.getSupplierUid());
                    cart.setProName(productInfoApi.getProductName());
                    cart.setProImgUrl(productInfoApi.getcarouselImgOne());
                    cart.setProUid(productInfoApi.getProductUid());
                    cart.setProNo(productInfoApi.getProductNo());
                    cart.setProUnit(productInfoApi.getUnitName());
                    cart.setProTag(productInfoApi.getTagsName());
                    cart.setCartStatus(CartStatusEnum.unPaid.getKey());
                    cart.setCartNumber(shopCartVo.getAmount());
                    cart.setShowPrice(productInfoApi.getShowPrice().longValue());
                    cart.setSupplierPrice(productInfoApi.getCostPrice().longValue());
                    cart.setCartAmount(cart.getCartNumber() * cart.getPrice());
                    cart.setPlatformUid(SecurityUtils.getCustomerPlatformUid());
                    cart.setCreateUid(SecurityUtils.getCustomerUid());
                    cart.setCreateName(SecurityUtils.getCustomerUserName());
                    cart.setCreateTime(new Date());
                    cart.setUpdateName(cart.getCreateName());
                    cart.setUpdateTime(cart.getCreateTime());
                    cart.setUpdateUid(cart.getCreateUid());
                    cart.setProImgUrl(addWebP(cart.getProImgUrl()));
                    cart.setSkuImgUrl(addWebP(cart.getSkuImgUrl()));
                    if (save(cart)) {
                        shopCartVo.setCartId(cart.getCartUid());
                        //操作日志
                        orderInfoService.sendLogMsg(ProductOptType.addCar.getKey(), cart.getPlatformUid(), cart.getCreateUid(),cart.getProType(), cart.getProUid(), cart.getCartNumber());

                        return shopCartVo;
                    } else {
                        throw new BaseException("购买商品失败，请重试");
                    }
                }
                throw new BaseException("购买商品失败，请重试");
            } catch (Exception e) {
                e.printStackTrace();

                if (e instanceof BaseException) {
                    throw e;
                } else {
                    throw new BaseException("系统服务繁忙，请稍后再试！");
                }
            }

    }

    @Override
    public ShopCartVo buyDdl(ShopCartVo shopCartVo) {

       return null;
    }

    @Override
    public ShopCartVo buyCoupon(ShopCartVo shopCartVo) {
        return null;
    }


    /**
     * 查询客户未结算的购物车数据
     *
     * @return
     */
    @Override
    public List<OrderShopCart> listByStoreId(Long storeId) {
        userLimit();
        QueryWrapper<OrderShopCart> queryWrapper = Wrappers.query();
        queryWrapper.eq("CREATE_UID", SecurityUtils.getCustomerUid());
        queryWrapper.eq("IS_SHOW_IN_CART", CartIsShowEnum.show.getKey());
        queryWrapper.eq("CART_STATUS", CartStatusEnum.unPaid.getKey());
        queryWrapper.eq("STORE_ID", storeId);
        queryWrapper.orderByDesc("CART_UID");
        return orderShopCartMapper.selectList(queryWrapper);
    }

    /**
     * 查询客户指定的购物车数据
     *
     * @param shopCartVo
     * @return
     */
    @Override
    public List<OrderShopCart> list(ShopCartVo shopCartVo) {
        userLimit();
        QueryWrapper<OrderShopCart> queryWrapper = Wrappers.query();
        queryWrapper.eq("CREATE_UID", SecurityUtils.getCustomerUid());
        //queryWrapper.eq("IS_SHOW_IN_CART",CartIsShowEnum.show.getKey());
        queryWrapper.eq("CART_STATUS", CartStatusEnum.unPaid.getKey());
        queryWrapper.eq("STORE_ID", shopCartVo.getStoreId());
        queryWrapper.in("CART_UID", shopCartVo.getCartList());
        queryWrapper.orderByDesc("CART_UID");
        return orderShopCartMapper.selectList(queryWrapper);
    }

    /**
     * 统计购物车数量
     *
     * @return
     */
    @Override
    public int cartCount(Long storeId) {
        userLimit();
        ShopCartVo vo = new ShopCartVo();
        vo.setCustomerId(SecurityUtils.getCustomerUid());
        vo.setStoreId(storeId);
        Long count = orderShopCartMapper.sumCartNumber(vo);
        if (ObjectUtil.isNotEmpty(count)) {
            return count.intValue();
        }
        return 0;
    }

    /**
     * 修改购物车结算状态
     *
     * @param cartUids
     * @return
     */
    @Override
    public boolean updateShopCartToSettled(List<Long> cartUids) {
        ShopCartVo vo = new ShopCartVo();
        vo.setSettlementStatus(CartStatusEnum.paid.getKey());
        vo.setCartList(cartUids);
        orderShopCartMapper.updateSettleStatus(vo);
        return true;
    }

    @Override
    public OrderShopCartVo getCart(Long carUids) {
        OrderShopCart cart = this.getById(carUids);
        if (ObjectUtil.isEmpty(cart)) {
            throw new BaseException("没有找到购物车信息");
        }
        OrderShopCartVo vo = new OrderShopCartVo();
        BeanUtils.copyProperties(cart, vo);

        return vo;
    }

    @Override
    public List<OrderShopCart> getCartList(List<Long> carUids) {
        if (carUids.size()>0) {
            QueryWrapper<OrderShopCart> queryWrapper = Wrappers.query();
            queryWrapper.eq("CART_STATUS", CartStatusEnum.unPaid.getKey());
            queryWrapper.in("CART_UID", carUids);
            return orderShopCartMapper.selectList(queryWrapper);
        }
        return new ArrayList<>();
    }

}
